package net.xiaosaguo.mymall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xiaosaguo.common.utils.PageUtils;
import net.xiaosaguo.common.utils.Query;
import net.xiaosaguo.mymall.product.dao.AttrGroupDao;
import net.xiaosaguo.mymall.product.entity.AttrEntity;
import net.xiaosaguo.mymall.product.entity.AttrGroupEntity;
import net.xiaosaguo.mymall.product.service.AttrGroupService;
import net.xiaosaguo.mymall.product.service.AttrService;
import net.xiaosaguo.mymall.product.vo.AttrGroupWithAttrVO;
import net.xiaosaguo.mymall.product.vo.SpuItemAttrGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author x
 */
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * description: 根据商品三级分类查询属性分组，默认 0 为查询全部
     *
     * @param params    分页参数
     * @param catelogId 商品三级分类 id
     * @return net.xiaosaguo.common.utils.PageUtils
     * @author xiaosaguo
     * @date 2020/06/13 09:34
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        // 模糊查询
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(wrapper -> wrapper.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        // 如果不是默认，就根据商品三级分类 ID 筛选
        if (catelogId != 0) {
            queryWrapper.eq("catelog_id", catelogId);
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }


    /**
     * description: 获取分类下所有分组&关联属性
     *
     * @param catelogId 商品分类 ID
     * @return 所有关联该属性分组的属性
     * @author xiaosaguo
     * @date 2020/6/15 19:56:46
     */
    @Override
    public List<AttrGroupWithAttrVO> getAttrGroupWithAttrByCategoryId(Long catelogId) {
        // 1. 根据分类获取该分类下所有分组信息
        List<AttrGroupEntity> attrGroupList = this.lambdaQuery().eq(AttrGroupEntity::getCatelogId, catelogId).list();
        return attrGroupList.stream().map(attrGroup -> {
            AttrGroupWithAttrVO attrGroupWithAttrVO = new AttrGroupWithAttrVO();
            BeanUtils.copyProperties(attrGroup, attrGroupWithAttrVO);
            // 2. 查询每个分组下的属性
            List<AttrEntity> attrList = attrService.getAttrByAttrGroup(attrGroupWithAttrVO.getAttrGroupId());
            attrGroupWithAttrVO.setAttrs(attrList);
            return attrGroupWithAttrVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SpuItemAttrGroupVO> getAttrGroupWithAttrBySpuId(Long spuId, Long catalogId) {
        // 1. 根据 spuId 查出对应的所有属性分组，以及属性分组下的所有属性，及属性的值

        // 1.1 当前分组有多少属性分组 pms_product_attr_value  groupName、attrName、attrValue
        return this.baseMapper.getAttrGroupWithAttrBySpuId(spuId, catalogId);

    }
}